package doantotnghiep.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import doantotnghiep.business.CustomerBusiness;
import doantotnghiep.model.Customer;
import doantotnghiep.modelDTO.CustomerDTO;

@Controller
public class CustomerManagController {

	@Autowired
	private CustomerBusiness customerBusiness;
	
	private CustomerDTO customerDTO;
	
	private Customer customer;
	
	@ModelAttribute("CustomerDTO")
	public CustomerDTO getCustomerObject() {
		return new CustomerDTO();
	}

	@RequestMapping(value = "/viewCustomer", method = RequestMethod.GET)
	public ModelAndView viewAllCustomer() {
		List<Customer> customers;
		List<CustomerDTO> customerDTOs = new ArrayList<CustomerDTO>();
		if(customerBusiness != null){
			customers = customerBusiness.listCustomer();
			if(customers != null){
				for (Customer c : customers) {
					customerDTO = new CustomerDTO();
					customerDTO.setAddress(c.getAddress());
					customerDTO.setFullName(c.getFullName());
					customerDTO.setTelNumber(c.getTelNumber());
					customerDTO.setId(c.getId());
					customerDTOs.add(customerDTO);
				}	
				Map<String, Object> listAllCustomer = new HashMap<String, Object>();
				listAllCustomer.put("customerDTOs", customerDTOs);
				return new ModelAndView("viewCustomer", "listAllCustomer",listAllCustomer);
			}
		}
		return new ModelAndView("viewCustomer","",null);
	}

	@RequestMapping(value = "/addCustomer", method = RequestMethod.POST)
	public String addCustomer(HttpServletRequest request, ModelMap model,
			@ModelAttribute("CustomerDTO") CustomerDTO customerDTO) {
		if (customerDTO != null) {
			customer = new Customer(); 
			customer.setAddress(customerDTO.getAddress());
			customer.setFullName(customerDTO.getFullName());
			customer.setTelNumber(customerDTO.getTelNumber());
			this.customerBusiness.addCustomer(customer);
		} 
		return "redirect:/viewCustomer";
	}

	@RequestMapping(value = "/addCustomer", method = RequestMethod.GET)
	public ModelAndView addCustomer() {
		return new ModelAndView("addCustomer","",null);
	}

	@RequestMapping(value = "/editCustomer", method = RequestMethod.GET)
	public ModelAndView editCustomer(HttpServletRequest request, ModelMap model,int id) {
		customer = customerBusiness.getCustomerById(id);
		if(customer != null){
		   customerDTO = new CustomerDTO();
		   customerDTO.setId(customer.getId());
		   customerDTO.setAddress(customer.getAddress());
		   customerDTO.setFullName(customer.getFullName());
		   customerDTO.setTelNumber(customer.getTelNumber());
		   return new ModelAndView("updateCustomer", "customerDTO", customerDTO);
		}
		return new ModelAndView("viewCustomer","",null);
	}

	@RequestMapping(value = "/editCustomer", method = RequestMethod.POST)
	public String editCustomer(HttpServletRequest request, ModelMap model,
			@ModelAttribute("CustomerDTO") CustomerDTO customerDTO) {
		if (customerDTO != null) {
			   customer = new Customer();
			   customer.setId(customerDTO.getId());
			   customer.setAddress(customerDTO.getAddress());
			   customer.setFullName(customerDTO.getFullName());
			   customer.setTelNumber(customerDTO.getTelNumber());
			this.customerBusiness.saveCustomerChanged(customer);
		}
		return "redirect:/viewCustomer";
	}

	@RequestMapping(value = "/deleteCustomer", method = RequestMethod.GET)
	public String deleteCustomer(HttpServletRequest request, ModelMap model,
			  int id) {
		customerBusiness.deleteCustomer(id);
		return "redirect:/viewCustomer";
	}
	
	/*receive require search a customer from database*/
	@RequestMapping(value = "/searchCustomer", method = RequestMethod.GET)
	public ModelAndView searchCustomer(HttpServletRequest request, ModelMap model,
			  String key) {
		byte[] tmp = key.getBytes();
		String value="";
		try {
			value = new String(tmp,"UTF-8");
			request.setCharacterEncoding("UTF-8");
			 
			System.out.println(value);
		} catch (UnsupportedEncodingException e) {
			
			e.printStackTrace();
		}
		List<Customer> customers;
		List<CustomerDTO> customerDTOs = new ArrayList<CustomerDTO>();
		customers = customerBusiness.getCustomerByKey(value);
		Map<String, Object> customerModel = new HashMap<String, Object>();
		if(customers!=null){
			for(Customer c: customers){
				CustomerDTO customerDTO = new CustomerDTO();
				customerDTO.setId(c.getId());
				customerDTO.setAddress(c.getAddress());
				customerDTO.setFullName(c.getFullName());
				customerDTO.setTelNumber(c.getTelNumber());
				customerDTOs.add(customerDTO);	
			}
			customerModel.put("customerDTOs",customerDTOs);
		}
		return new ModelAndView("customerSearchResult","customerModel",customerModel);
	}

}
